<?php
class Product_Model_Package
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_Package();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ud_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('pp_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $ud_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('pp_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	public function getByPpid($pp_id){	
		$select = $this->_table->select()->where('pp_id = ?', $pp_id);	 
		return $this->_table->getAdapter()->fetchRow($select);		
	}
	
	public function getByValue($value,$field){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}	
	
	public function getByCond($cond,$type,$order='pp_id',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['pp_name']) && $cond['pp_name']){
			$select->where('pp_name = ?',$cond['pp_name']);
		}
		if(isset($cond['status']) && $cond['status'] !== ''){
			$select->where('status = ?',$cond['status']);
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);
		$sql = $select->__toString();
		$row = $this->_table->getAdapter()->fetchAll($sql);
		return $row;	
	}
	
	public function getAll($warehouseId,$customerId = null){
		$select = $this->_table->select()->where("warehouse_id = ?",$warehouseId);
		if(!empty($customerId)){
			$select->where("customer_id in (?)",$customerId);
		}
		return $this->_table->fetchAll($select);
	}
	
	
}